package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.dao.jdbc.DepartmentJdbc;
import com.wuseguang.springcloud.dao.mapper.DepartmentMapper;
import com.wuseguang.springcloud.dao.repository.DepartmentRepository;
import com.wuseguang.springcloud.service.DepartmentService;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 学校部门 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-01-04
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private DepartmentRepository departmentRepository;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private DepartmentJdbc departmentJdbc;

    @Override
    public Result findSchoolDepartment(String schoolId,Integer type) {
        logger.info("查询学校的部门信息");
        //学生部门信息
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();

       try{

           //判断是学生还是老师
           if(type ==1){
               logger.info("这里是老师");

               String sql  = "select id,name from `department` WHERE `school_id` = ?";
               logger.info("查询部门l sql = {}",sql);
               //部门信息
               list = departmentJdbc.queryListMap(sql,schoolId);
               //循环部门信息
               for (Map<String,Object> map : list) {
                   logger.info("部门信息 map = {}",map.toString());
                   sql = "select id,name,other from `grade` where `department_id`  = ?";
                   logger.info("查询年级sql = {}",sql);
                   List<Map<String,Object>> gradeList = departmentJdbc.queryListMap(sql,map.get("id"));
                   for (Map<String,Object> gradeMap :gradeList) {
                       logger.info("年级信息 map = {}",gradeMap.toString());
                       sql = "select c.id,c.`name`   from `classes` c LEFT JOIN  `grade`  g on(g.id=c.`grade_id` ) where c.`grade_id` = ? AND c.graduation=0";
                       logger.info("查询班级信息 sql = {}",sql);
                       List<Map<String,Object>> classList = departmentJdbc.queryListMap(sql,gradeMap.get("id"));
                       gradeMap.put("list",classList);
                   }
                   map.put("list",gradeList);
               }
           }else {
               logger.info("这里是学生");
               //查询年级SQL
               String sql = "select id,name,other from `grade` WHERE `school_id` =?";
               list = departmentJdbc.queryListMap(sql,schoolId);
               for (Map<String,Object> map :list) {
                   sql ="select c.id,c.`name`   from `classes` c  where c.`grade_id` =? AND c.graduation=0 ";
                   logger.info("查询班级 sql = {}",sql );
                   List<Map<String,Object>> classlist = departmentJdbc.queryListMap(sql,map.get("id"));
                   map.put("list",classlist);
               }


           }

       }catch (Exception e){
           return new Result();
       }

        return new Result(list);
    }
}
